/**
 * File ImageUtils.java
 * ---------------------------------------------------------
 *
 * Copyright (C) 2012 David Bauske (david.bauske@googlemail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * - The origin of the software must not be misrepresented.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * TL;DR: As long as you clearly give me credit for this Software, you are free to use as you like, even in commercial software, but don't blame me
 * 		if it breaks something.
 */
package net.cl.util.images;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

/**
 * Contains static methods used for image operations.
 *
 * @author David Bauske
 */
public class ImageUtils
{
	/**
	 * Rescales a buffered image.
	 * 
	 * Credit for scaling idea goes to some guy on the internet whose name I unfortunately forgot. If you are the
	 * original author, please contact me so I can add your name here.
	 *
	 * @param pImg The image to convert. Must not be null.
	 * @param pTargetWidth The width in pixels to convert the image to
	 * @param pTargetHeight The height inpixels to convert the image to
	 * @param pInterpolationValue An object indicating which interpolation to use. Must be one of the following
	 *            constants: {@link java.awt.RenderingHints#VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
	 *            {@link java.awt.RenderingHints#VALUE_INTERPOLATION_BILINEAR} or
	 *            {@link java.awt.RenderingHints#VALUE_INTERPOLATION_BICUBIC}
	 * @param pHigherQuality <i>true</i> to perform a multi-pass operation, which is slower, but results in higher image
	 *            quality. Pass <i>false</i> to use only one step, which is quite fast.
	 * @return A rescaled instance of pImg.
	 * @throws NullPointerException if pImg is null.
	 */
	public static BufferedImage getScaledImage(BufferedImage pImg,
			int pTargetWidth,
			int pTargetHeight,
			Object pInterpolationValue,
			boolean pHigherQuality)
	{
		int type = (pImg.getTransparency() == Transparency.OPAQUE) ?
				BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
		BufferedImage ret = pImg;
		int w, h;
		if(pHigherQuality)
		{
			// Use multi-step technique
			w = pImg.getWidth();
			h = pImg.getHeight();
		}
		else
		{
			// Use one-step technique
			w = pTargetWidth;
			h = pTargetHeight;
		}

		do
		{
			if(pHigherQuality && w != pTargetWidth)
			{
				if(w > pTargetWidth)
				{
					w /= 2;
					if(w < pTargetWidth)
					{
						w = pTargetWidth;
					}
				}
				else
				{
					w *= 2;
					if(w > pTargetWidth)
					{
						w = pTargetWidth;
					}
				}
			}

			if(pHigherQuality && h != pTargetHeight)
			{
				if(h > pTargetHeight)
				{
					h /= 2;
					if(h < pTargetHeight)
					{
						h = pTargetHeight;
					}
				}
				else
				{
					h *= 2;
					if(h > pTargetHeight)
					{
						h = pTargetHeight;
					}
				}
			}

			BufferedImage tmp = new BufferedImage(w, h, type);
			Graphics2D g2 = tmp.createGraphics();
			g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, pInterpolationValue);
			g2.drawImage(ret, 0, 0, w, h, null);
			g2.dispose();

			ret = tmp;
		}
		while(w != pTargetWidth || h != pTargetHeight);

		return ret;
	}
}
